home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Toolbox / Visual Basic Toolbox (P.I.E.)(1996).ISO / toolkit / riruf1 / rufdb.bas < prev    next >
Encoding:
BASIC Source File  |  1995-03-23  |  15.7 KB  |  617 lines

  1. Option Explicit
  2. Declare Function SendMessage Lib "User" (ByVal hWnd As Integer, ByVal wMsg As Integer, ByVal wParam As Integer, ByVal lParam As Any) As Long
  3. Global TheDatabase As Database
  4. Global TheDynaset As dynaset
  5. Global TheSnapShot As snapshot
  6. Global nCurrentID%
  7.  
  8. 'RufLogin form variables
  9. Global bLogin%
  10. Global sUserName$, sPassword$
  11.  
  12. 'RufDB Form variables
  13. Global bDBChange%, bRufDbEnd%
  14. Global sDBPath$
  15. Dim sInsert$, sSystem$, sNewStr$
  16. Dim bComma%
  17.  
  18. Global Const WM_USER = &H400
  19. Global Const CB_FINDSTRING = (WM_USER + 12)
  20. Global Const CB_FINDSTRINGEXACT = (WM_USER + 24)
  21.  
  22. Function AddQuote (sStr As String) As String
  23.     Dim nOff%, nStart%
  24.     sNewStr = sStr
  25.  
  26.     nStart = 1
  27.     nOff = InStr(nStart, sNewStr, "`", 1)
  28.     While nOff > 0
  29.         Mid(sNewStr, nOff, 1) = "'"
  30.         nStart = nStart + 1
  31.         nOff = InStr(nStart, sNewStr, "`", 1)
  32.     Wend
  33.     AddQuote = sNewStr
  34. End Function
  35.  
  36. Function AddQuoteV (vStr As Variant) As String
  37.     Dim nOff%, nStart%
  38.     sNewStr = vStr
  39.     sNewStr = RTrim$(sNewStr)
  40.  
  41.     nStart = 1
  42.     nOff = InStr(nStart, sNewStr, "`", 1)
  43.     While nOff > 0
  44.         Mid(sNewStr, nOff, 1) = "'"
  45.         nStart = nStart + 1
  46.         nOff = InStr(nStart, sNewStr, "`", 1)
  47.     Wend
  48.     AddQuoteV = sNewStr
  49.  
  50. End Function
  51.  
  52. Sub AddToInsert (sValue As String, bLit As Integer)
  53.     If bComma Then
  54.         sInsert = sInsert & ", "
  55.     Else
  56.         bComma = True
  57.     End If
  58.  
  59.     If bLit Then
  60.         sValue = RemoveQuote(sValue)
  61.         sInsert = sInsert & " '" & RTrim$(sValue) & "'"
  62.     Else
  63.         sInsert = sInsert & RTrim$(sValue)
  64.     End If
  65.  
  66. End Sub
  67.  
  68. Sub AddToUpdate (sName As String, sValue As String, bLit As Integer)
  69.     If bComma Then
  70.         sInsert = sInsert & ", "
  71.     Else
  72.         bComma = True
  73.     End If
  74.  
  75.     If bLit Then
  76.         sValue = RemoveQuote(sValue)
  77.         sInsert = sInsert & sName & " = '" & RTrim$(sValue) & "'"
  78.     Else
  79.         sInsert = sInsert & sName & " = " & RTrim$(sValue)
  80.     End If
  81.  
  82.  
  83. End Sub
  84.  
  85. Sub AddToUpdateV (sName As String, vValue As Variant, bLit As Integer)
  86.     Dim sTmp$
  87.     If bComma Then
  88.         sInsert = sInsert & ", "
  89.     Else
  90.         bComma = True
  91.     End If
  92.  
  93.     If bLit Then
  94.         sTmp = vValue
  95.         sTmp = RemoveQuote(sTmp)
  96.         sInsert = sInsert & sName & " = '" & RTrim$(sTmp) & "'"
  97.     Else
  98.         sInsert = sInsert & sName & " = " & RTrim$(sTmp)
  99.     End If
  100.  
  101. End Sub
  102.  
  103. Sub CheckAndSaveCbo (cboCtrl As ComboBox, sTable$, sField$, bPad%)
  104.     Dim sBuff$, sVal$, nIndex%
  105.  
  106.     If bPad Then
  107.         sVal = Format$(RTrim$(cboCtrl.Text), "##.00")
  108.         cboCtrl.Text = sVal
  109.     Else
  110.         sVal = RTrim$(cboCtrl.Text)
  111.     End If
  112.     If Len(sVal) > 0 Then
  113.         nIndex = SendMessage(cboCtrl.hWnd, CB_FINDSTRINGEXACT, -1, sVal)
  114.  
  115.         If nIndex = -1 Then
  116.             cboCtrl.AddItem sVal
  117.             CreateInsert sTable
  118.             AddToInsert sVal, False
  119.             sBuff = GetInsertStatement()
  120.             TheDatabase.Execute sBuff
  121.         End If
  122.     End If
  123.  
  124. End Sub
  125.  
  126. Sub CompactDB (sCompDBName As String)
  127.     On Error GoTo errhandler
  128.     Const sTmpDB$ = "rufcomdb.mdb"
  129.     Dim sLocation$, sShortName$
  130.     Dim n%, nErr%
  131.     Dim sMsg$
  132.  
  133.     n = InStr(1, sDBPath, sCompDBName, 1)
  134.     sLocation = Left$(sDBPath, n - 1)
  135.  
  136.     n = InStr(1, sCompDBName, ".", 1)
  137.     sShortName = Left(sCompDBName, n - 1)
  138.  
  139.     sMsg = "Are you sure you want to compact the database?"
  140.     sMsg = sMsg & " All other users should exit the  " & TheAppTitle & " before continuing."
  141.     If Not AskUser(sMsg) Then
  142.         Exit Sub
  143.     End If
  144.  
  145.     HourglassCursor
  146.  
  147.     nErr = False
  148.     CompactDatabase sLocation & sCompDBName, sLocation & sTmpDB
  149.  
  150.     If nErr <> True Then ' if compacting was successful then
  151.         InformUser "Database has been compacted successfully!"
  152.  
  153.         ' deleting .bak and .ldb files
  154.         Kill sLocation & sShortName & ".bak"
  155.         Kill sLocation & "rufcomdb.ldb"
  156.  
  157.         ' making a backup of the original and
  158.         ' renaming the compacted to the actual database
  159.         Name sLocation & sCompDBName As sLocation & sShortName & ".bak"
  160.         Name sLocation & sTmpDB As sLocation & sCompDBName
  161.     End If
  162.     ArrowCursor
  163.     Exit Sub
  164.  
  165. errhandler:
  166.     nErr = True
  167.     If Err <> 53 Then 'skil file not found error
  168.         ArrowCursor
  169.         DatabaseError
  170.     End If
  171.     Resume Next
  172.  
  173. End Sub
  174.  
  175. Sub CreateInsert (sTable As String)
  176.     sInsert = "Insert into " & sTable & " values ( "
  177.     bComma = False
  178. End Sub
  179.  
  180. Sub CreateUpdate (sTable As String)
  181.     sInsert = "Update " & sTable & " Set "
  182.     bComma = False
  183. End Sub
  184.  
  185. Sub DatabaseError ()
  186.     Dim sMsg$
  187.  
  188.     Select Case Err
  189.  
  190.         Case 3000
  191.             sMsg = "Database is exclusively locked."
  192.         Case 3001
  193.             sMsg = "Enter the database path & name."
  194.         Case 3002
  195.             sMsg = "Couldn't start session."
  196.         Case 3003
  197.             sMsg = "Couldn't start transaction; too many transactions already nested."
  198.         Case 3004
  199.             sMsg = "Couldn't find database"
  200.         Case 3005
  201.             sMsg = "Isn't a valid database name."
  202.         Case 3006
  203.             sMsg = "Database is exclusively locked."
  204.         Case 3007
  205.             sMsg = "Couldn't open database."
  206.         Case 3013
  207.             sMsg = "Couldn't rename installable ISAM file."
  208.         Case 3024
  209.             sMsg = "Couldn't find file."
  210.         Case 3025
  211.             sMsg = "Can't open any more files."
  212.         Case 3026
  213.             sMsg = "Not enough space on disk."
  214.         Case 3027
  215.             sMsg = "Couldn't update; database is read-only."
  216.         Case 3028
  217.             sMsg = "Couldn't initialize data access because file 'SYSTEM.MDA' couldn't be opened."
  218.         Case 3029
  219.             sMsg = "Not a valid account name or password."
  220.         Case 3035
  221.             sMsg = "Out of memory."
  222.         Case 3036
  223.             sMsg = "Database has reached maximum size."
  224.         Case 3037
  225.             sMsg = "Can't open any more tables or queries."
  226.         Case 3038
  227.             sMsg = "Out of memory."
  228.         Case 3040
  229.             sMsg = "Disk I/O error during read."
  230.         Case 3041
  231.             sMsg = "Incompatible database version."
  232.         Case 3042
  233.             sMsg = "Out of MS-DOS file handles."
  234.         Case 3043
  235.             sMsg = "Disk or network error."
  236.         Case 3044
  237.             sMsg = "Isn't a valid path."
  238.         Case 3045
  239.             sMsg = "Couldn't use; file already in use."
  240.         Case 3046
  241.             sMsg = "Couldn't save; currently locked by another user."
  242.         Case 3048
  243.             sMsg = "Can't open any more databases."
  244.         Case 3049
  245.             sMsg = "Database is corrupted or isn't a Microsoft Access database."
  246.         Case 3050
  247.             sMsg = "Couldn't lock file; SHARE.EXE hasn't been loaded."
  248.         Case 3051
  249.             sMsg = "Couldn't open file."
  250.         Case 3052
  251.             sMsg = "MS-DOS file sharing lock count exceeded.  You need to increase the number of locks installed with SHARE.EXE."
  252.         Case 3053
  253.             sMsg = "Too many client tasks."
  254.         Case 3055
  255.             sMsg = "Not a valid file name."
  256.         Case 3056
  257.             sMsg = "Couldn't repair this database."
  258.         Case Else
  259.             sMsg = "Database error: " & Err
  260.     End Select
  261.  
  262.     Beep
  263.     MsgBox sMsg, MB_OK + MB_ICONSTOP, TheAppTitle
  264.  
  265. End Sub
  266.  
  267. Sub ExecuteInsert (sBuff As String)
  268.     TheDatabase.Execute (sBuff)
  269. End Sub
  270.  
  271. Function GetCBOID (cboCtrl As ListBox, sField As String) As Long
  272.  
  273.     If cboCtrl.ListIndex = -1 Then
  274.         Beep
  275.         MsgBox "No " & sField & " record has been selected!", MB_ICONEXCLAMATION + MB_OK, TheAppTitle
  276.         Exit Function
  277.     End If
  278.     GetCBOID = cboCtrl.ItemData(cboCtrl.ListIndex)
  279.  
  280. End Function
  281.  
  282. Function GetCmdLineStr (sStr As String) As String
  283.     Dim nLen%, nPos%, nEnd%
  284.     Dim sCmd$
  285.  
  286.     sCmd = Command$
  287.     nLen = Len(sStr)
  288.     nPos = InStr(1, sCmd, sStr, 1)
  289.     If nPos Then
  290.         nPos = nPos + nLen
  291.         nEnd = InStr(nPos, sCmd, " ", 1)
  292.         If nEnd Then
  293.             nLen = nEnd - nPos
  294.             GetCmdLineStr = Mid$(sCmd, nPos, nLen)
  295.         Else
  296.             GetCmdLineStr = Mid$(sCmd, nPos)
  297.         End If
  298.     Else
  299.         GetCmdLineStr = ""
  300.     End If
  301.  
  302. End Function
  303.  
  304. Sub GetDynaset (sDef As String)
  305.     Dim qDef As querydef
  306.  
  307.     Set qDef = TheDatabase.OpenQueryDef(sDef)
  308.     Set TheDynaset = qDef.CreateDynaset()
  309.     qDef.Close
  310.  
  311. End Sub
  312.  
  313. Function GetID (ByVal sFieldName As String) As Long
  314.     Dim lNewID, lVal As Long
  315.     Dim ssSystem As snapshot
  316.     Dim sBuff$
  317.  
  318.     sBuff$ = "select " & sFieldName & " From " & sSystem & " Where RecNo = 1;"
  319.     Set ssSystem = TheDatabase.CreateSnapshot(sBuff)
  320.  
  321.     If Not IsNull(ssSystem(sFieldName)) Then
  322.         lNewID = ssSystem(sFieldName)
  323.     Else
  324.         lNewID = 1
  325.     End If
  326.  
  327.     lVal = lNewID + 1
  328.  
  329.     If Not ssSystem.EOF Then
  330.  
  331.         sBuff$ = "Update " & sSystem & " Set " & sFieldName & " = " & Str$(lVal) & " Where RecNo = 1;"
  332.         TheDatabase.Execute sBuff$
  333.         GetID = lNewID
  334.         Exit Function
  335.  
  336.     End If
  337.  
  338.     GetID = -1
  339.  
  340. End Function
  341.  
  342. Function GetInsertStatement () As String
  343.     sInsert = sInsert & " )"
  344.     GetInsertStatement = sInsert
  345. End Function
  346.  
  347. Function GetLBID (lstCtrl As ListBox, sField As String) As Long
  348.  
  349.     If lstCtrl.ListIndex = -1 Then
  350.         InformUser "No " & sField & " record has been selected!"
  351.         GetLBID = -1
  352.         Exit Function
  353.     End If
  354.     GetLBID = lstCtrl.ItemData(lstCtrl.ListIndex)
  355.  
  356. End Function
  357.  
  358. Sub GetSnapshot (sDef As String)
  359.     Dim qDef As querydef
  360.  
  361.     Set qDef = TheDatabase.OpenQueryDef(sDef)
  362.     Set TheSnapShot = qDef.CreateSnapshot()
  363.     qDef.Close
  364.  
  365. End Sub
  366.  
  367. Function GetUpdateStatement (sWhere As String) As String
  368.     sInsert = sInsert & sWhere
  369.     GetUpdateStatement = sInsert
  370. End Function
  371.  
  372. Function KeyFound (sTable$, sField$, sValue$) As Integer
  373.     Dim sBuff$
  374.     Dim ssData As snapshot
  375.  
  376.     sBuff = "Select " & sField & " from " & sTable & " where " & sField & " = '" & sValue & "';"
  377.     Set ssData = TheDatabase.CreateSnapshot(sBuff)
  378.     If Not ssData.EOF Then
  379.         KeyFound = True
  380.     Else
  381.         KeyFound = False
  382.     End If
  383.     ssData.Close
  384.  
  385. End Function
  386.  
  387. Sub LoadCombo (sQDef As String, lDefault As Long, cboCtrl As ComboBox, bParam As Integer, sSeparator As String, bClear As Integer)
  388.     On Error GoTo loadcomboErr
  389.     Dim dsData As snapshot
  390.     Dim qDef As querydef
  391.     Dim sLine$, i%, nIndex%, sSep$
  392.  
  393.     HourglassCursor
  394.     nIndex = -1
  395.     Set qDef = TheDatabase.OpenQueryDef(sQDef)
  396.     If bParam Then
  397.         qDef!Param = lDefault
  398.     End If
  399.     Set dsData = qDef.CreateSnapshot()
  400.     qDef.Close
  401.  
  402.     If Len(sSeparator) = 0 Then
  403.         sSep = " "
  404.     Else
  405.         sSep = sSeparator & " "
  406.     End If
  407.  
  408.     If bClear Then
  409.         cboCtrl.Clear
  410.     End If
  411.  
  412.     While Not dsData.EOF
  413.         If Not IsNull(dsData(0)) Then
  414.  
  415.             sLine = ""
  416.             For i = 1 To dsData.Fields.Count - 1
  417.                 If Not IsNull(dsData(i)) Then
  418.                     sLine = sLine & AddQuoteV(dsData(i))
  419.                     If i < dsData.Fields.Count - 1 Then
  420.                         sLine = sLine & sSep
  421.                     End If
  422.                 End If
  423.             Next
  424.             cboCtrl.AddItem sLine
  425.             cboCtrl.ItemData(cboCtrl.NewIndex) = dsData(0)
  426.             If lDefault <> -1 Then
  427.                 If lDefault = dsData(0) Then
  428.                     nIndex = cboCtrl.NewIndex
  429.                 End If
  430.             End If
  431.  
  432.         End If
  433.         dsData.MoveNext
  434.     Wend
  435.     dsData.Close
  436.  
  437.     If nIndex <> -1 Then
  438.         cboCtrl.ListIndex = nIndex
  439.     End If
  440.     ArrowCursor
  441.     Exit Sub
  442.  
  443. loadcomboErr:
  444.     ArrowCursor
  445.     GetErrorMsg Err
  446.     Exit Sub
  447. End Sub
  448.  
  449. Sub LoadListBox (sQDef As String, lDefault As Long, lstCtrl As ListBox, bParam As Integer, sSeparator As String)
  450.     On Error GoTo loadlistErr
  451.     Dim dsData As snapshot
  452.     Dim qDef As querydef
  453.     Dim sLine$, i%, nIndex%, sSep$, nCnt%
  454.  
  455.     HourglassCursor
  456.     nIndex = -1
  457.     nCnt = 1
  458.     Set qDef = TheDatabase.OpenQueryDef(sQDef)
  459.     If bParam Then
  460.         qDef!Param = lDefault
  461.     End If
  462.     Set dsData = qDef.CreateSnapshot()
  463.     qDef.Close
  464.  
  465.     If Len(sSeparator) = 0 Then
  466.         sSep = " "
  467.     Else
  468.         sSep = sSeparator & " "
  469.     End If
  470.  
  471.     If dsData.Fields.Count = 1 Then
  472.         nCnt = 0
  473.     End If
  474.  
  475.     lstCtrl.Clear
  476.  
  477.     While Not dsData.EOF
  478.         If Not IsNull(dsData(0)) Then
  479.  
  480.             sLine = ""
  481.             For i = nCnt To dsData.Fields.Count - 1
  482.                 If Not IsNull(dsData(i)) Then
  483.                     sLine = sLine & AddQuoteV(dsData(i))
  484.                     If i < dsData.Fields.Count - 1 Then
  485.                         sLine = sLine & sSep
  486.                     End If
  487.                 End If
  488.             Next
  489.             lstCtrl.AddItem sLine
  490.             If nCnt <> 0 Then
  491.                 lstCtrl.ItemData(lstCtrl.NewIndex) = dsData(0)
  492.                 If lDefault <> -1 Then
  493.                     If lDefault = dsData(0) Then
  494.                         nIndex = lstCtrl.NewIndex
  495.                     End If
  496.                 End If
  497.             End If
  498.  
  499.         End If
  500.         dsData.MoveNext
  501.     Wend
  502.     dsData.Close
  503.  
  504.     If nIndex <> -1 Then
  505.         lstCtrl.ListIndex = nIndex
  506.     End If
  507.     ArrowCursor
  508.  
  509.     Exit Sub
  510. loadlistErr:
  511.     ArrowCursor
  512.     GetErrorMsg Err
  513.     Exit Sub
  514.  
  515. End Sub
  516.  
  517. Function PasswordOK () As Integer
  518.     Dim ssData As snapshot
  519.     Dim sBuff$, sTmp
  520.  
  521.     PasswordOK = False
  522.     sBuff = "Select PersonID, Password from Personnel where UserName = '" & sUserName & "'"
  523.     Set ssData = TheDatabase.CreateSnapshot(sBuff)
  524.  
  525.     If ssData.EOF Then
  526.         InformUser "Invalid login!"
  527.     Else
  528.         sTmp = ssData("Password")
  529.         sTmp = Encrypt(sTmp)
  530.         If StrComp(sPassword, sTmp) <> 0 Then
  531.             InformUser "Invalid login!"
  532.         Else
  533.             PasswordOK = True
  534.             nCurrentID = ssData("PersonID")
  535.         End If
  536.     End If
  537.  
  538. End Function
  539.  
  540. Function RemoveQuote (sStr As String) As String
  541.     Dim nOff%, nStart%
  542.  
  543.     sNewStr = sStr
  544.     nStart = 1
  545.     nOff = InStr(nStart, sNewStr, "'", 1)
  546.     While nOff > 0
  547.         Mid(sNewStr, nOff, 1) = "`"
  548.         nStart = nStart + 1
  549.         nOff = InStr(nStart, sNewStr, "'", 1)
  550.     Wend
  551.  
  552.     RemoveQuote = sNewStr
  553. End Function
  554.  
  555. Sub ScanCombo (ByVal lID As Long, cboCtrl As ComboBox)
  556.     Dim bFound%, i%
  557.  
  558.     If lID <> -1 Then
  559.         bFound = False
  560.         i = 0
  561.         While Not bFound And i < cboCtrl.ListCount
  562.             If lID = cboCtrl.ItemData(i) Then
  563.                 cboCtrl.ListIndex = i
  564.                 bFound = True
  565.             End If
  566.             i = i + 1
  567.         Wend
  568.     Else
  569.         cboCtrl.ListIndex = -1
  570.     End If
  571.  
  572. End Sub
  573.  
  574. Sub ScanListBox (ByVal lID As Long, lstCtrl As ListBox)
  575.     Dim bFound%, i%
  576.  
  577.     If lID <> -1 Then
  578.         bFound = False
  579.         i = 0
  580.         While Not bFound And i < lstCtrl.ListCount
  581.             If lID = lstCtrl.ItemData(i) Then
  582.                 lstCtrl.ListIndex = i
  583.                 bFound = True
  584.             End If
  585.             i = i + 1
  586.         Wend
  587.     Else
  588.         lstCtrl.ListIndex = -1
  589.     End If
  590.  
  591. End Sub
  592.  
  593. Sub ScanMultiListBox (ByVal lID As Long, lstCtrl As ListBox)
  594.     Dim bFound%, i%
  595.  
  596.     If lID <> -1 Then
  597.         bFound = False
  598.         i = 0
  599.         While Not bFound And i < lstCtrl.ListCount
  600.             If lID = lstCtrl.ItemData(i) Then
  601.                 lstCtrl.ListIndex = i
  602.                 lstCtrl.Selected(i) = True
  603.                 bFound = True
  604.             End If
  605.             i = i + 1
  606.         Wend
  607.     Else
  608.         lstCtrl.ListIndex = -1
  609.     End If
  610.  
  611. End Sub
  612.  
  613. Sub SetSystemDB (sStr As String)
  614.     sSystem = sStr
  615. End Sub
  616.  
  617.